home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Display Manager SDK / Sample Code / Display Changed CWPro3 / Source / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-20  |  12.3 KB  |  613 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        events.c
  4. #
  5. #        This segment handles the basic event calls.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            1/19/99        ewa     update to CWPro3 and universal interfaces                     
  13. #            10/12/95    MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Events.h>
  29. #include <ToolUtils.h>
  30. #include <Gestalt.h>
  31. #include <OSUtils.h>
  32. #include <Palettes.h>
  33.  
  34. #include "App.h"
  35. #include "Proto.h"
  36.  
  37.  
  38. //----------------------------------------------------------------------
  39. //    Globals -
  40. //----------------------------------------------------------------------
  41.  
  42. extern Boolean        gInBackground;
  43. extern Boolean        gDone;
  44. extern Boolean        gHasAbout;        // have an about box?
  45.  
  46.  
  47. //----------------------------------------------------------------------
  48. //
  49. //    EventLoop - main entry and loop for all event processing
  50. //
  51. //
  52. //----------------------------------------------------------------------
  53.  
  54. void EventLoop(void)
  55. {
  56.     EventRecord        event;
  57.     RgnHandle        cursorRgn;
  58.     Boolean            gotEvent;
  59.         
  60.     gDone = false;
  61.     cursorRgn = NewRgn();
  62.         
  63.     do 
  64.     {
  65.         gotEvent = WaitNextEvent(everyEvent, &event, MyGetSleep(), cursorRgn);
  66.                                     
  67.         if (gotEvent)
  68.             DoEvent(&event);
  69.  
  70.                         
  71.     } while (!gDone);
  72.         
  73.     DisposeRgn(cursorRgn);
  74. }
  75.  
  76.  
  77. //----------------------------------------------------------------------
  78. //
  79. //    MyGetSleep - return sleep value based upon whether or not the app
  80. //                 is in the background.
  81. //
  82. //----------------------------------------------------------------------
  83.  
  84. short MyGetSleep(void)
  85. {
  86.     short        sleep = 30;
  87.     
  88.     if (gInBackground)
  89.         sleep = 1L;
  90.  
  91.     return sleep;
  92.  
  93. }
  94.  
  95.  
  96. //----------------------------------------------------------------------
  97. //
  98. //    CustomWindowEvent - Handles custom procs assigned to a window. 
  99. //                        Different window kinds can easily have unique event
  100. //                        handlers, ie. floaters, dialogs, documentprocs
  101. //----------------------------------------------------------------------
  102.  
  103. void CustomWindowEvent(short eventType,WindowRef window,void *refCon)
  104. {
  105.     CustomProc        theProc;
  106.     short            kind;
  107.     DocHnd            doc;
  108.     
  109.     if (nil == window)
  110.         return;
  111.         
  112.     kind = GetWindKind(window);
  113.     if (kind < kDocKind || kind > kAboutKind)     // not our window
  114.         return;
  115.  
  116.     doc = (DocHnd)GetWRefCon(window);
  117.     
  118.     if (doc != nil) 
  119.     {
  120.         HLockHi((Handle)doc);
  121.         switch(eventType) 
  122.         {
  123.             case kIdleProc:
  124.                 theProc = (**doc).idleProc;
  125.                 break;
  126.                 
  127.             case kMenuProc:
  128.                 theProc = (**doc).mMenuProc;
  129.                 break;
  130.  
  131.             case kInContentProc:
  132.                 theProc = (**doc).inContentProc;
  133.                 break;
  134.  
  135.             case kInGoAwayProc:
  136.                 theProc = (**doc).inGoAwayProc;
  137.                 break;
  138.                 
  139.             case kInZoomProc:
  140.                 theProc = (**doc).inZoomProc;
  141.                 break;
  142.                 
  143.             case kInGrowProc:
  144.                 theProc = (**doc).inGrowProc;
  145.                 break;
  146.                 
  147.             case kMUpProc:
  148.                 theProc = (**doc).mUpProc;
  149.                 break;
  150.                 
  151.             case kKeyProc:
  152.                 theProc = (**doc).keyProc;
  153.                 break;
  154.             
  155.             case kActivateProc:
  156.                 theProc = (**doc).activateProc;
  157.                 break;
  158.  
  159.             case kUpdateProc:
  160.                 theProc = (**doc).updateProc;
  161.                 break;
  162.  
  163.             default:
  164.                 theProc = nil;
  165.                 break;
  166.         }        
  167.     
  168.         if (theProc != nil) 
  169.         {            
  170.             (*theProc)(window,refCon);
  171.             HUnlock((Handle)doc);
  172.     
  173.         }
  174.     }    
  175.     
  176.     
  177. }
  178.  
  179.  
  180. //----------------------------------------------------------------------
  181. //
  182. //    DoEvent - event dispatcher, called by eventloop
  183. //                
  184. //
  185. //----------------------------------------------------------------------
  186.  
  187. void DoEvent(EventRecord *event)
  188. {
  189.     OSErr            err;
  190.     short            kind;
  191.     long            menuChoice;
  192.     Point            thePoint;
  193.     Boolean            active;
  194.     WindowRef        window;
  195.     
  196.     
  197.     window = FrontWindow();
  198.     
  199.     switch(event->what) 
  200.     {
  201.         case nullEvent:
  202.             CustomWindowEvent(kIdleProc, window, nil);
  203.             break;
  204.             
  205.         case mouseDown:
  206.             HandleMouseDown(event);
  207.             break;
  208.                             
  209.         case mouseUp:
  210.             break;
  211.                             
  212.         case keyDown:
  213.         case autoKey:
  214.             if (event->modifiers & cmdKey)             //    is cmd key down
  215.             { 
  216.                 menuChoice = MenuKey(event->message & charCodeMask);
  217.                 kind = GetWindKind(window);
  218.                 if (kind < kDocKind || kind > kFloatKind)             // not our window
  219.                     HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  220.                 else    
  221.                     CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  222.             }
  223.             break;
  224.                             
  225.         case activateEvt:
  226.             active = event->modifiers & activeFlag;
  227.             CustomWindowEvent(kActivateProc, (WindowRef)event->message, &active);
  228.             break;
  229.                             
  230.         case updateEvt:
  231.             UpdateWindow((WindowRef)event->message);
  232.             break;
  233.                             
  234.         case diskEvt:
  235.             if (HiWord(event->message) != noErr) 
  236.             {
  237.                 SetPt(&thePoint, 50, 50);
  238.                 err = DIBadMount(thePoint, event->message);
  239.             }
  240.             break;
  241.                             
  242.         case osEvt:
  243.             switch ((event->message >> 24) & 0x0FF) 
  244.             {        
  245.                 case suspendResumeMessage:    
  246.                     gInBackground = event->message & resumeFlag;
  247.                     active = gInBackground;
  248.                     CustomWindowEvent(kActivateProc, FrontWindow(), &active);
  249.                     break;
  250.             }
  251.             break;
  252.     
  253.         case kHighLevelEvent:
  254.             AEProcessAppleEvent(event);
  255.             break;
  256.     }
  257.  
  258. }            
  259.  
  260.  
  261.  
  262.  
  263. //----------------------------------------------------------------------
  264. //
  265. //    DoIdle - handle Idle events
  266. //                
  267. //
  268. //----------------------------------------------------------------------
  269.  
  270. void DoIdle(WindowRef window, void *refCon)
  271. {
  272.  
  273. }
  274.  
  275.  
  276. //----------------------------------------------------------------------
  277. //
  278. //    HandleMouseDown - 
  279. //                
  280. //
  281. //----------------------------------------------------------------------
  282.  
  283. void HandleMouseDown(EventRecord *event)
  284. {
  285.     long            menuChoice;
  286.     short            thePart;
  287.     short            kind;
  288.     WindowRef        window;
  289.         
  290.  
  291.     thePart = FindWindow(event->where,&window);
  292.         
  293.     switch(thePart) 
  294.     {
  295.         case inMenuBar:
  296.             menuChoice = MenuSelect(event->where);
  297.             window = FrontWindow();
  298.             kind = GetWindKind(window);
  299.             if (kind < kDocKind || kind > kAboutKind)             // not our window
  300.                 HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  301.             else    
  302.                 CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  303.             break;
  304.  
  305.         case inContent:
  306.             if (window != FrontWindow())
  307.                 SelectWindow(window);
  308.             else
  309.                 CustomWindowEvent(kInContentProc, window, &event->where);
  310.     
  311.             break;
  312.  
  313.         case inSysWindow:
  314.             SystemClick(event,window);
  315.             break;
  316.                                                 
  317.         case inDrag:
  318.             if (window != FrontWindow())
  319.                 SelectWindow(window);
  320.             DragWindow(window, event->where,&qd.screenBits.bounds);
  321.             break;
  322.                         
  323.         case inGoAway:
  324.             if (TrackGoAway(window, event->where))
  325.                 RemoveWindow(window);
  326.             break;
  327.                         
  328.         case inZoomIn:
  329.         case inZoomOut:
  330.             if (TrackBox(window,event->where,thePart)) 
  331.                 CustomWindowEvent(kInZoomProc, window,&thePart);
  332.             break;
  333.                         
  334.         case inGrow:
  335.             CustomWindowEvent(kInGrowProc, window, &event->where);
  336.             break;
  337.     }
  338.     
  339. }
  340.  
  341.  
  342. //----------------------------------------------------------------------
  343. //
  344. //    HandleMenuChoice - 
  345. //                
  346. //
  347. //----------------------------------------------------------------------
  348.  
  349. void HandleMenuChoice(WindowRef window, void *refCon)
  350. {
  351.     long         menuChoice;
  352.     short        item, menu;
  353.     short        daRefNum;
  354.     Rect        bounds;
  355.     Str255        daName;
  356.     WindowRef    newWindow;
  357.     
  358.     
  359.     menuChoice = *(long *)refCon;
  360.     
  361.     item = LoWord(menuChoice);
  362.     menu = HiWord(menuChoice);
  363.  
  364.     switch(menu) 
  365.     {
  366.         case mApple:
  367.             switch(item) 
  368.             {
  369.                 case iAbout:
  370.                     if (!gHasAbout) 
  371.                     {
  372.                         SetRect(&bounds, 2, 40 ,352 ,140);
  373.                         newWindow = CreateWindow(nil, nil, &bounds, "\pAbout", true,
  374.                                             documentProc, kAboutKind, (WindowPtr)-1, true, nil);
  375.                         gHasAbout = true;
  376.                     }                        
  377.                     break;
  378.                     
  379.                 default:
  380.                     GetMenuItemText(GetMenuHandle(mApple), item, daName);
  381.                     daRefNum = OpenDeskAcc(daName);
  382.                     break;
  383.             }    
  384.             break;
  385.                     
  386.         case mFile:
  387.             switch(item) 
  388.             {
  389.                 case iNew:
  390.                     SetRect(&bounds, 100, 100, 300, 300);
  391.                     newWindow = CreateWindow(128, nil, &bounds, nil, false,
  392.                                             documentProc, kDocKind, (WindowPtr)-1,
  393.                                             true, nil );
  394.                     break;
  395.                 case iOpen:
  396.                     DoOpenNew();
  397.                     break;
  398.                         
  399.                 case iClose:
  400.                     RemoveWindow(FrontWindow());
  401.                     break;    
  402.                         
  403.                 case iQuit:
  404.                     gDone = true;
  405.                     break;
  406.                     
  407.                 default:
  408.                     break;    
  409.             }
  410.             break;
  411.                     
  412.         default:
  413.             break;    
  414.         
  415.     }
  416.     
  417.     HiliteMenu(0);
  418.     
  419. }
  420.  
  421.  
  422. //----------------------------------------------------------------------
  423. //
  424. //    HandleContentClick - 
  425. //                
  426. //
  427. //----------------------------------------------------------------------
  428.  
  429. void HandleContentClick(WindowRef window, void *refCon)
  430. {
  431.     ControlRef            control;
  432.     ControlActionUPP    scrollUPP;
  433.     Point                mouse;
  434.     short                value;
  435.     short                thePart;
  436.     short                oldSetting;
  437.     short                horzScroll,vertScroll;
  438.     DocHnd                doc;
  439.  
  440.     doc = (DocHnd)GetWRefCon(window);
  441.     if (doc != nil) 
  442.     {
  443.         SetPort(window);
  444.         mouse = *(Point *)refCon;
  445.         GlobalToLocal(&mouse);
  446.         horzScroll = vertScroll = 0;
  447.  
  448.         if ((thePart = FindControl(mouse, window, &control)) != 0) 
  449.         {
  450.             switch(thePart) 
  451.             {
  452.                 case kControlIndicatorPart:
  453.                     oldSetting = GetControlValue(control);
  454.                     thePart = TrackControl(control, mouse, 0L);
  455.                     if (thePart != 0) {
  456.                         value = oldSetting - GetControlValue(control);
  457.                         if (value != 0)
  458.                         {
  459.                             if (control == (**doc).hScroll)
  460.                                 horzScroll = value ;
  461.                             if (control == (**doc).vScroll)
  462.                                 vertScroll = value;
  463.                             MyScrollPicture(window, horzScroll, vertScroll);
  464.                         }
  465.                     }
  466.                     break;
  467.                     
  468.                 case kControlUpButtonPart:
  469.                 case kControlDownButtonPart:
  470.                 case kControlPageUpPart:
  471.                 case kControlPageDownPart:
  472.                     scrollUPP = NewControlActionProc(ScrollActionProc);
  473.                     value = TrackControl(control, mouse, scrollUPP);
  474.                     break;
  475.     
  476.             }
  477.         }
  478.     }
  479.  
  480. }
  481.  
  482.  
  483. //----------------------------------------------------------------------
  484. //
  485. //    HandleZoomClick - 
  486. //                
  487. //
  488. //----------------------------------------------------------------------
  489.  
  490. void HandleZoomClick(WindowRef window, void *refCon)
  491. {
  492.     short            part;
  493.     DocHnd            doc;
  494.     
  495.     doc = (DocHnd)GetWRefCon(window);
  496.     if (doc != nil) 
  497.     {
  498.         part = *(short *)refCon;
  499.         SetPort(window);
  500.         
  501.         EraseRect(&window->portRect);
  502.         ZoomWindow(window,part,true);
  503.         ClipRect(&window->portRect);
  504.         InvalRect(&window->portRect);
  505.         
  506.         HideControl((**doc).hScroll);
  507.         HideControl((**doc).vScroll);
  508.         
  509.         AdjustScrollbars(window, true);
  510.         
  511.         ShowControl((**doc).hScroll);
  512.         ShowControl((**doc).vScroll);
  513.         
  514.     }
  515.     
  516. }
  517.  
  518.  
  519. //----------------------------------------------------------------------
  520. //
  521. //    HandleGrow - 
  522. //                
  523. //
  524. //----------------------------------------------------------------------
  525.  
  526. void HandleGrow(WindowRef window, void *refCon)
  527. {
  528.     Rect            gIRect;
  529.     Rect            limitRect;
  530.     long            growSize;
  531.         
  532.     limitRect = (**GetGrayRgn()).rgnBBox;
  533.     limitRect.left += 125;
  534.     limitRect.top += 125;
  535.  
  536.     growSize = GrowWindow(window, *(Point *)refCon, &limitRect);
  537.     if (growSize) 
  538.     {
  539.         SetPort(window);    
  540.         gIRect = window->portRect;
  541.         gIRect.top = gIRect.bottom - kScrollWidth;
  542.         gIRect.left = gIRect.right - kScrollWidth;     
  543.         EraseRect(&gIRect);
  544.         SizeWindow(window,LoWord(growSize),HiWord(growSize),true);
  545.         
  546.         ClipRect(&window->portRect);
  547.         AdjustScrollbars(window, true);
  548.         InvalRect(&window->portRect);
  549.     }
  550.  
  551. }
  552.  
  553.  
  554. //----------------------------------------------------------------------
  555. //
  556. //    UpdateWindow - update dispatcher for document windows.
  557. //                 
  558. //
  559. //----------------------------------------------------------------------
  560.  
  561. void UpdateWindow(WindowRef window) 
  562. {
  563.     GrafPtr        oldPort;
  564.     
  565.     
  566.     GetPort(&oldPort);
  567.     
  568.     SetPort(window);
  569.     BeginUpdate(window);
  570.     CustomWindowEvent(kUpdateProc, window, nil);
  571.     EndUpdate(window);
  572.     
  573.     SetPort(oldPort);
  574.  
  575. }
  576.  
  577.  
  578. //----------------------------------------------------------------------
  579. //
  580. //    DoActivate - 
  581. //                 
  582. //
  583. //----------------------------------------------------------------------
  584.  
  585. void DoActivate(WindowRef window, void *refCon)
  586. {
  587.     Boolean        becomingActive;
  588.     DocHnd        doc;
  589.     
  590.     SetPort(window);
  591.         
  592.     doc = (DocHnd)GetWRefCon(window);
  593.  
  594.     if(doc != nil && GetIsAppWindow(window)) 
  595.     {
  596.         becomingActive = *(Boolean *)refCon;
  597.         if (becomingActive) 
  598.         {
  599.             DrawGrowIcon(window);
  600.             ShowControl((**doc).hScroll);
  601.             ShowControl((**doc).vScroll);
  602.         }
  603.         else 
  604.         {
  605.             HideControl((**doc).hScroll);
  606.             HideControl((**doc).vScroll);
  607.             DrawGrowIcon(window);
  608.         }
  609.     }
  610.     
  611. }
  612.  
  613.